home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 17 controls / databindingdemo / adonetbindingform.vb < prev    next >
Text File  |  2002-03-16  |  8KB  |  219 lines

  1. Imports System.Data
  2. Imports System.Data.OleDb
  3.  
  4. Public Class AdoNetBindingForm
  5.     Inherits System.Windows.Forms.Form
  6.  
  7. #Region " Windows Form Designer generated code "
  8.  
  9.     Public Sub New()
  10.         MyBase.New()
  11.  
  12.         'This call is required by the Windows Form Designer.
  13.         InitializeComponent()
  14.  
  15.         'Add any initialization after the InitializeComponent() call
  16.  
  17.     End Sub
  18.  
  19.     'Form overrides dispose to clean up the component list.
  20.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  21.         If disposing Then
  22.             If Not (components Is Nothing) Then
  23.                 components.Dispose()
  24.             End If
  25.         End If
  26.         MyBase.Dispose(disposing)
  27.     End Sub
  28.     Friend WithEvents Label1 As System.Windows.Forms.Label
  29.     Friend WithEvents btnFirst As System.Windows.Forms.Button
  30.     Friend WithEvents lblRecord As System.Windows.Forms.Label
  31.     Friend WithEvents btnNext As System.Windows.Forms.Button
  32.     Friend WithEvents btnLast As System.Windows.Forms.Button
  33.     Friend WithEvents btnPrevious As System.Windows.Forms.Button
  34.     Friend WithEvents txtName As System.Windows.Forms.TextBox
  35.     Friend WithEvents Label2 As System.Windows.Forms.Label
  36.     Friend WithEvents txtState As System.Windows.Forms.TextBox
  37.     Friend WithEvents Label3 As System.Windows.Forms.Label
  38.     Friend WithEvents txtCity As System.Windows.Forms.TextBox
  39.  
  40.     'Required by the Windows Form Designer
  41.     Private components As System.ComponentModel.Container
  42.  
  43.     'NOTE: The following procedure is required by the Windows Form Designer
  44.     'It can be modified using the Windows Form Designer.  
  45.     'Do not modify it using the code editor.
  46.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  47.         Me.btnLast = New System.Windows.Forms.Button()
  48.         Me.txtCity = New System.Windows.Forms.TextBox()
  49.         Me.Label1 = New System.Windows.Forms.Label()
  50.         Me.btnPrevious = New System.Windows.Forms.Button()
  51.         Me.Label3 = New System.Windows.Forms.Label()
  52.         Me.btnFirst = New System.Windows.Forms.Button()
  53.         Me.btnNext = New System.Windows.Forms.Button()
  54.         Me.txtState = New System.Windows.Forms.TextBox()
  55.         Me.lblRecord = New System.Windows.Forms.Label()
  56.         Me.Label2 = New System.Windows.Forms.Label()
  57.         Me.txtName = New System.Windows.Forms.TextBox()
  58.         Me.SuspendLayout()
  59.         '
  60.         'btnLast
  61.         '
  62.         Me.btnLast.Location = New System.Drawing.Point(336, 88)
  63.         Me.btnLast.Name = "btnLast"
  64.         Me.btnLast.Size = New System.Drawing.Size(48, 32)
  65.         Me.btnLast.TabIndex = 11
  66.         Me.btnLast.Text = ">>"
  67.         '
  68.         'txtCity
  69.         '
  70.         Me.txtCity.Location = New System.Drawing.Point(208, 40)
  71.         Me.txtCity.Name = "txtCity"
  72.         Me.txtCity.Size = New System.Drawing.Size(160, 24)
  73.         Me.txtCity.TabIndex = 0
  74.         Me.txtCity.Text = ""
  75.         '
  76.         'Label1
  77.         '
  78.         Me.Label1.Location = New System.Drawing.Point(24, 16)
  79.         Me.Label1.Name = "Label1"
  80.         Me.Label1.Size = New System.Drawing.Size(152, 16)
  81.         Me.Label1.TabIndex = 1
  82.         Me.Label1.Text = "Name"
  83.         '
  84.         'btnPrevious
  85.         '
  86.         Me.btnPrevious.Location = New System.Drawing.Point(152, 88)
  87.         Me.btnPrevious.Name = "btnPrevious"
  88.         Me.btnPrevious.Size = New System.Drawing.Size(48, 32)
  89.         Me.btnPrevious.TabIndex = 8
  90.         Me.btnPrevious.Text = "<"
  91.         '
  92.         'Label3
  93.         '
  94.         Me.Label3.Location = New System.Drawing.Point(208, 16)
  95.         Me.Label3.Name = "Label3"
  96.         Me.Label3.Size = New System.Drawing.Size(152, 16)
  97.         Me.Label3.TabIndex = 1
  98.         Me.Label3.Text = "City"
  99.         '
  100.         'btnFirst
  101.         '
  102.         Me.btnFirst.Location = New System.Drawing.Point(104, 88)
  103.         Me.btnFirst.Name = "btnFirst"
  104.         Me.btnFirst.Size = New System.Drawing.Size(48, 32)
  105.         Me.btnFirst.TabIndex = 7
  106.         Me.btnFirst.Text = "<<"
  107.         '
  108.         'btnNext
  109.         '
  110.         Me.btnNext.Location = New System.Drawing.Point(288, 88)
  111.         Me.btnNext.Name = "btnNext"
  112.         Me.btnNext.Size = New System.Drawing.Size(48, 32)
  113.         Me.btnNext.TabIndex = 10
  114.         Me.btnNext.Text = ">"
  115.         '
  116.         'txtState
  117.         '
  118.         Me.txtState.Location = New System.Drawing.Point(376, 40)
  119.         Me.txtState.Name = "txtState"
  120.         Me.txtState.Size = New System.Drawing.Size(96, 24)
  121.         Me.txtState.TabIndex = 0
  122.         Me.txtState.Text = ""
  123.         '
  124.         'lblRecord
  125.         '
  126.         Me.lblRecord.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  127.         Me.lblRecord.Location = New System.Drawing.Point(200, 88)
  128.         Me.lblRecord.Name = "lblRecord"
  129.         Me.lblRecord.Size = New System.Drawing.Size(88, 32)
  130.         Me.lblRecord.TabIndex = 9
  131.         Me.lblRecord.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
  132.         '
  133.         'Label2
  134.         '
  135.         Me.Label2.Location = New System.Drawing.Point(376, 16)
  136.         Me.Label2.Name = "Label2"
  137.         Me.Label2.Size = New System.Drawing.Size(56, 16)
  138.         Me.Label2.TabIndex = 1
  139.         Me.Label2.Text = "State"
  140.         '
  141.         'txtName
  142.         '
  143.         Me.txtName.Location = New System.Drawing.Point(24, 40)
  144.         Me.txtName.Name = "txtName"
  145.         Me.txtName.Size = New System.Drawing.Size(168, 24)
  146.         Me.txtName.TabIndex = 0
  147.         Me.txtName.Text = ""
  148.         '
  149.         'AdoNetBindingForm
  150.         '
  151.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  152.         Me.ClientSize = New System.Drawing.Size(496, 149)
  153.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3, Me.txtCity, Me.Label2, Me.txtState, Me.btnFirst, Me.lblRecord, Me.btnNext, Me.btnLast, Me.btnPrevious, Me.Label1, Me.txtName})
  154.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  155.         Me.Name = "AdoNetBindingForm"
  156.         Me.Text = "AdoNetBindingForm"
  157.         Me.ResumeLayout(False)
  158.  
  159.     End Sub
  160.  
  161. #End Region
  162.  
  163.     Private Sub AdoNetBindingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  164.         CreateDataSet()
  165.         InitializeBinding()
  166.     End Sub
  167.  
  168.     Dim dsBiblio As New DataSet()
  169.     Dim bmb As BindingManagerBase
  170.  
  171.     ' creates a DataSet containing the Publishers table
  172.  
  173.     Sub CreateDataSet()
  174.         ' Open a connection to the Biblio.mdb database.
  175.         ' (You might have to provide a different connection string or path.)
  176.         Dim cn As New OleDbConnection(BiblioConnString)
  177.         cn.Open()
  178.         ' Create a data adapter on this connection.
  179.         Dim daPublishers As New OleDbDataAdapter("SELECT * FROM Publishers", cn)
  180.         ' Fill the DataSet object with data from the Publishers table.
  181.         daPublishers.Fill(dsBiblio, "Publishers")
  182.         ' Close the connection.
  183.         cn.Close()
  184.     End Sub
  185.  
  186.     ' initialize all fields for data binding
  187.  
  188.     Sub InitializeBinding()
  189.         ' Bind fields to database fields.
  190.         txtName.DataBindings.Add("Text", dsBiblio, "Publishers.Name")
  191.         txtCity.DataBindings.Add("Text", dsBiblio, "Publishers.City")
  192.         txtState.DataBindings.Add("Text", dsBiblio, "Publishers.State")
  193.         ' get a reference to the BindingManagerBase object.
  194.         bmb = Me.BindingContext(dsBiblio, "Publishers")
  195.  
  196.         ' Force a refresh.
  197.         bmb.Position = bmb.Count
  198.         bmb.Position = 0
  199.     End Sub
  200.  
  201.     ' navigational buttons
  202.  
  203.     Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
  204.         bmb.Position = 0
  205.     End Sub
  206.  
  207.     Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
  208.         bmb.Position -= 1
  209.     End Sub
  210.  
  211.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  212.         bmb.Position += 1
  213.     End Sub
  214.  
  215.     Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
  216.         bmb.Position = bmb.Count
  217.     End Sub
  218. End Class
  219.